<?php
/**
 * Copyright (c) 2006-2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 * 
 * Derived from CakePHP's TimeHelper Copyright (c) 2006,
 * Cake Software Foundation, Inc.
 * http://cakephp.org/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @todo Write some documentation.
 */

/**
 * Useful functions to manipulate dates.
 */
class Time
{
	/**
	 * 
	 * @return Integer
	 * @param $date Mixed
	 */
	static private function from_string($date)
	{
		return empty($date) ? time() : (
			is_integer($date) ? $date : strtotime($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function date($date=null)
	{
		return strftime(t("%Y-%m-%d"), self::from_string($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function datetime($date=null)
	{
		$date = self::from_string($date);
		return strftime(t("%Y-%m-%d %H:%M", 'global.time'), $date);
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function nice_date($date=null)
	{
		$date = self::from_string($date);
		return self::is_this_year($date) ?
			ucfirst(strftime(t("%b %e", 'global.time'), $date)) :
			ucfirst(strftime(t("%b %e, %Y", 'global.time'), $date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function nice($date=null)
	{
		$date = self::from_string($date);
		return strftime(t("%A %B %e %Y, %I:%M%p", 'global.time'), $date);
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function nice_short($date=null)
	{
		$date = self::from_string($date);
		
		if (self::is_today($date)) {
			return sprintf(t('today, %s', 'global.time'), date("H:i", $date));
		}
		elseif (self::was_yesterday($date)) {
			return sprintf(t('yesterday, %s', 'global.time'), date( "H:i", $date));
		}
		else
		{
			$str = self::is_this_year($date) ?
				t("%B %e, %I:%M%p", 'global.time') :
				t("%B %e %Y, %I:%M%p", 'global.time');
			return strftime($str, $date);
		}
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function short($date=null)
	{
		$date = self::from_string($date);
		$str  = self::is_this_year($date) ?
			t("%b %e, %I:%M%p", 'global.time') :
			t("%b %e %Y, %I:%M%p", 'global.time');
		return strftime($str, $date);
	}
	
	/**
	 * 
	 * @return Boolean
	 * @param $date Mixed
	 */
	static function is_in_future($date)
	{
		return (self::from_string($date) > time());
	}
	
	/**
	 * 
	 * @return Boolean
	 * @param $date Mixed
	 */
	static function is_today($date)
	{
		return date('Y-m-d', self::from_string($date)) == date('Y-m-d', time());
	}	
	
	/**
	 * 
	 * @return Boolean
	 * @param $date Mixed
	 */
	static function is_this_year($date)
	{
		return date('Y', self::from_string($date)) == date('Y', time());
	}
	
	/**
	 * 
	 * @return Boolean
	 * @param $date Mixed
	 */
	static function was_yesterday($date)
	{
		return date('Y-m-d', self::from_string($date)) == date('Y-m-d', strtotime('yesterday'));
	}
	
	/**
	 * 
	 * @return Boolean
	 * @param $date Mixed
	 */
	static function is_tomorrow($date)
	{
		return date('Y-m-d', self::from_string($date)) == date('Y-m-d', strtotime('tomorrow'));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function to_atom($date=null)
	{
		return date('Y-m-d\TH:i:s\Z', self::from_string($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed[optional]
	 */
	static function to_rss($date=null)
	{
		return date('r', self::from_string($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed[optional]
	 */
	static function SQL($date=null)
	{
		return date('Y-m-d H:i:s', self::from_string($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed[optional]
	 */
	static function sql_date($date=null)
	{
		return date('Y-m-d', self::from_string($date));
	}
	
	/**
	 * 
	 * @return String
	 * @param $date Mixed[optional]
	 */
	static function sql_time($date=null)
	{
		return date('H:i:s', self::from_string($date));
	}
	
	/**
	 * Returns the elapsed time since a given date.
	 * 
	 * @return String
	 * @param $date Mixed
	 */
	static function ago($date)
	{
		$date     = self::from_string($date);
		$diff     = time() - $date;
		$day_diff = round($diff / 86400);
		
		if ($day_diff == 0)
		{
			if     ($diff <= 60)    return t('just now', 'global.time');
			elseif ($diff <= 120)   return t('a minute ago', 'global.time');
			elseif ($diff <= 3600)  return sprintf(t('%d minutes ago', 'global.time'), round($diff / 60));
			elseif ($diff <= 7200)  return t('an hour ago', 'global.time');
			elseif ($diff <= 86400) return sprintf(t('%d hours ago', 'global.time'), round($diff / 3600));
		}
		elseif ($day_diff == 1) {
			return t('Yesterday', 'global.time');
		}
		elseif ($day_diff <= 7) {
			return sprintf(tn('%d day ago', '%d days ago', $day_diff, 'global.time'), $day_diff);
		}
		elseif ($day_diff <= 31)
		{
			$weeks_ago = round($day_diff / 7);
			return sprintf(tn('%d week ago', '%d weeks ago', $weeks_ago, 'global.time'), $weeks_ago);
		}
		return self::nice_date($date);
	}
	
	/**
	 * Returns the age of something (eg. a person) from it's birthdate.
	 * 
	 * @return Integer
	 * @param $birthday
	 */
	function age($birthday)
	{
		$birthday = self::from_string($birthday);
	    $year  = date("Y") - date('Y', $birthday);
	    $month = date("m") - date('m', $birthday);
	    $day   = date("d") - date('d', $birthday);
	    return ($day < 0 or $month < 0) ? $year - 1 : $year;
	}
}
?>